package com.lhd.egweibo3.controller;

import com.lhd.egweibo3.common.response.JsonResult;
import com.lhd.egweibo3.common.response.StatusCode;
import com.lhd.egweibo3.mapper.CommentMapper;
import com.lhd.egweibo3.pojo.dto.CommentDTO;
import com.lhd.egweibo3.pojo.entity.Comment;
import com.lhd.egweibo3.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * @author lhd
 * @date 2024/4/25
 * @apiNote
 */
@Api(tags = "3.评论模块")
@Slf4j
@RestController
@RequestMapping(value ="v1/comment/")
public class CommentController {

    @Autowired
    private CommentMapper commentMapper;

    @PostMapping("insert")
    @ApiOperation(value ="发表评论功能")
    public JsonResult getCommentMapper(@RequestBody CommentDTO commentDTO, @ApiIgnore HttpSession session) {
        log.debug("", commentDTO);
        UserVO userVO = (UserVO)session.getAttribute("user");
        if (userVO == null) {
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO, comment);
        comment.setUserId(userVO.getId());
        comment.setCreated(new Date());
        commentMapper.insert(comment);
        return JsonResult.ok();
    }

    @PostMapping("selectByWeiboId")
    @ApiOperation(value ="评论列表功能")
    @ApiModelProperty(name = "id",value = "微博id",required = true,dataType = "Long")
    public JsonResult selectByWeiboId(Long id) {
        return JsonResult.ok(commentMapper.selectByWeiboId(id));
    }
}
